<?php
namespace My\Model;

use Utuo\UtuoModel;

class WechatUserModel extends UtuoModel {
	
	const USER_TYPE_STANDARD = 0; // 普通会员
	const USER_TYPE_AGENT = 1; // 代理商
	const USER_TYPE_FRANCHISED_DEALER = 2; // 金牌会员
	
	/**
	 * 顺序固定  0.普通会员/1.代理商/2.金牌会员 (从0开始的有序数列)
	 * @var unknown
	 */
	static $USER_TYPES = array(
		0 => "普通会员",
		1 => "代理商",
		2 => "金牌会员",
	);
	
	/**
	 * 获得用户类型名
	 * @param array( 'agentLevel' => 0 )
	 */
	public static function getUserTypeName( $type, $cid, $uid ) {
		$jogTitleList = DistributionShopJobTitleModel::getJobTitleList( $cid );
		$userRole = new UserRole();
		$roles = $userRole->type($type)->getUserRoles();
		$string = array();
		/* 商城配置 */
		static $distributionShop;
		if ( !$distributionShop ) {
		    $distributionShop = M("DistributionShop")->where(array('cid'=>$cid))->find();
		    $distributionShop['level_config'] and $distributionShop['level_config'] = unserialize($distributionShop['level_config']);
		}
		$levelIO = $distributionShop['level_config']; // 等级开关
		/* 角色名显示 */
		foreach ( $roles as $role ) {
			if ( $role == self::USER_TYPE_STANDARD ) {
			    $wechatUser = M("WechatUser")->where(array('id' => $uid))->field('level')->find();
				if ( $levelIO[self::USER_TYPE_STANDARD] ) { // 分等级
				    $userAlias = $jogTitleList[$role]['levelAlias'];
				    if ( $userAlias and $userAlias[$wechatUser['level']] ) {
				        $string[$role] = $userAlias[$wechatUser['level']];
				    } else {
				        $string[$role] = "--";
				    }
				} else { // 不分等级
				    $string[$role] = $jogTitleList[$role]['name'];
				}
				
			} elseif( $role == self::USER_TYPE_FRANCHISED_DEALER ) {
			    $wechatUser = M("Dealer")->where(array('wechat_uid' => $uid))->field('level')->find();
			    if ( $levelIO[self::USER_TYPE_FRANCHISED_DEALER] ) { // 分等级
			        $userAlias = $jogTitleList[$role]['levelAlias'];
			        if ( $userAlias and $userAlias[$wechatUser['level']] ) {
			            $string[$role] = $userAlias[$wechatUser['level']];
			        } else {
			            $string[$role] = "--";
			        }
			    } else { // 不分等级
			        $string[$role] = $jogTitleList[$role]['name'];
			    }
				
			} elseif ( $role == self::USER_TYPE_AGENT ) {
				$agent = M("Agent")->where(array('wechat_uid' => $uid))->field('level')->find();
				if ( $jogTitleList[$role]['hasLevel'] or $levelIO[self::USER_TYPE_AGENT] ) { // 分等级
				    $agentAlias = $jogTitleList[$role]['levelAlias'];
				    if ( $agentAlias and $agentAlias[$agent['level']] ) {
				        $string[$role] = $agentAlias[$agent['level']];
				    } else {
				        $string[$role] = AgentModel::$agentLevel[$agent['level']];
				    }
				} else { // 不分等级
				    $string[$role] = $jogTitleList[$role]['name'];
				}
			}
		}
		ksort($string);
		if ( count($string) > 1 ) {
			unset($string[self::USER_TYPE_STANDARD]);
		}
		
		return join(' 兼 ', $string);
	}
	
}